package com.anytron.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.anytron.config.ApoloConfigureCache;
import com.anytron.model.AppApollo;
import com.anytron.model.AppConfigs;
import com.anytron.model.AppInfo;
import com.anytron.model.AppNode;
import com.anytron.model.AppTimer;
import com.anytron.model.NodeInfo;
import com.anytron.model.res.NameSpaceContent;
import com.anytron.service.AppApolloService;
import com.anytron.service.AppConfigsService;
import com.anytron.service.AppInfoService;
import com.anytron.service.AppNodeService;
import com.anytron.service.CoreService;
import com.anytron.service.NodeInfoService;
import com.anytron.service.TimerLogsService;
import com.anytron.utils.ApolloServiceUtils;
import com.anytron.utils.LinuxShell;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CoreServiceImpl implements CoreService {

	@Autowired
	AppInfoService appInfoService;
	@Autowired
	AppConfigsService appConfigsService;
	@Autowired
	AppApolloService appApolloService;
	@Autowired
	AppNodeService appNodeService;
	@Autowired
	NodeInfoService nodeInfoService;
	@Autowired
	TimerLogsService timerLogsService;
	@Autowired
	ApolloServiceUtils apolloServiceUtils;
	@Value("${des.key}")
	private String key;
	
	@Override
	public boolean executeTask(AppTimer appTimer, int type) {
		Long appId = appTimer.getAppId();
		AppInfo appInfo = appInfoService.getByKey(appId);
		String typeName = 1 == type ? "定时" : "手动";
		if (appInfo == null) {
			log.error("执行任务失败:[" + appTimer.getAppId() + "] 类型:" + typeName);
			return false;
		}
		// 查询应用下属配置列表
		AppConfigs configs = new AppConfigs();
		configs.setAppId(appId);
		configs.setNeedEffective(1);
		List<AppConfigs> list = appConfigsService.list(configs);
		// 查询应用apollo配置
		AppApollo apollo = new AppApollo();
		apollo.setAppId(appId);
		AppApollo appApollo = appApolloService.getOne(apollo);
		List<NameSpaceContent> contents = apolloServiceUtils.getContentByAppConfigs(appApollo, list);
		// 筛选出可以进行更新的配置项
		contents = contents.stream().filter(item -> {
			String pin = appApollo.getApolloAppId() 
					+ "#" + appApollo.getApolloEnv() 
					+ "#" + appApollo.getApolloCluster()
					+ "#" + item.getNameSpace();
			item.setPin(pin);
			return ApoloConfigureCache.isNeedUpdate(item);
		}).collect(Collectors.toList());
		// 查询应用所部署的服务器信息
		AppNode appNode = new AppNode();
		appNode.setAppId(appId);
		//查询后循环信息
		DES des = new DES(Mode.ECB, Padding.PKCS5Padding, key.getBytes());
		List<NodeInfo> nodeInfos = appNodeService.list(appNode).stream().map(item -> nodeInfoService.getByKey(item.getNodeId())).collect(Collectors.toList());
		for (NodeInfo nodeInfo : nodeInfos) {
			//远程登录服务器
			LinuxShell linuxShell = new LinuxShell();
			String nodePasw = nodeInfo.getNodePasw();
			String password = des.decryptStr(nodePasw);
			int port = Integer.parseInt(nodeInfo.getNodePort());
			linuxShell.login(nodeInfo.getNodeHost(),port, nodeInfo.getNodeUser(), password);
			//执行脚本上传配置内容 执行每个配置的上传,完成后对当前服务器的生效命令进行执行,之后再关闭连接
			contents.forEach(content->{
				try {
					// TODO
					linuxShell.executeShell("");
				} catch (Exception e) {
					log.error(e.getMessage(),e);
				}
			});
		}
		return false;
	}

}
